package com.ruoyi.wms.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.wms.domain.WmsStockPosition;
import com.ruoyi.wms.service.IWmsStockPositionService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 仓位Controller
 * 
 * @author ruoyi
 * @date 2022-02-24
 */
@RestController
@RequestMapping("/wms/stockPosition")
public class WmsStockPositionController extends BaseController
{
    @Autowired
    private IWmsStockPositionService wmsStockPositionService;

    /**
     * 查询仓位列表
     */
    @PreAuthorize(hasPermi = "wms:stockPosition:list")
    @GetMapping("/list")
    public TableDataInfo list(WmsStockPosition wmsStockPosition)
    {
        startPage();
        List<WmsStockPosition> list = wmsStockPositionService.selectWmsStockPositionList(wmsStockPosition);
        return getDataTable(list);
    }

    /**
     * 导出仓位列表
     */
    @PreAuthorize(hasPermi = "wms:stockPosition:export")
    @Log(title = "仓位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsStockPosition wmsStockPosition) throws IOException
    {
        List<WmsStockPosition> list = wmsStockPositionService.selectWmsStockPositionList(wmsStockPosition);
        ExcelUtil<WmsStockPosition> util = new ExcelUtil<WmsStockPosition>(WmsStockPosition.class);
        util.exportExcel(response, list, "仓位数据");
    }

    /**
     * 获取仓位详细信息
     */
    @PreAuthorize(hasPermi = "wms:stockPosition:query")
    @GetMapping(value = "/{stockPosUuid}")
    public AjaxResult getInfo(@PathVariable("stockPosUuid") String stockPosUuid)
    {
        return AjaxResult.success(wmsStockPositionService.selectWmsStockPositionByStockPosUuid(stockPosUuid));
    }

    /**
     * 新增仓位
     */
    @PreAuthorize(hasPermi = "wms:stockPosition:add")
    @Log(title = "仓位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsStockPosition wmsStockPosition)
    {
        wmsStockPosition.setCreateBy(SecurityUtils.getUsername());
        return toAjax(wmsStockPositionService.insertWmsStockPosition(wmsStockPosition));
    }

    /**
     * 修改仓位
     */
    @PreAuthorize(hasPermi = "wms:stockPosition:edit")
    @Log(title = "仓位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsStockPosition wmsStockPosition)
    {
        return toAjax(wmsStockPositionService.updateWmsStockPosition(wmsStockPosition));
    }

    /**
     * 删除仓位
     */
    @PreAuthorize(hasPermi = "wms:stockPosition:remove")
    @Log(title = "仓位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{stockPosUuids}")
    public AjaxResult remove(@PathVariable String[] stockPosUuids)
    {
        return toAjax(wmsStockPositionService.deleteWmsStockPositionByStockPosUuids(stockPosUuids));
    }

    @Log(title = "仓位导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<WmsStockPosition> util = new ExcelUtil<WmsStockPosition>(WmsStockPosition.class);
        List<WmsStockPosition> stockPositionList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = wmsStockPositionService.importItem(stockPositionList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<WmsStockPosition> util = new ExcelUtil<WmsStockPosition>(WmsStockPosition.class);
        util.importTemplateExcel(response, "用户数据");
    }
}
